<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ include file="../common/tags.jsp" %>  
<%String path = request.getContextPath();%>
<script type="text/javascript">	
//获取被选中的会员卡id
function releaseFavourable(type){
	var customerId = '${memberCardVO.customerId}';
	var customerName = '${memberCardVO.customerName}';
	var a = $("input[name='selectids']:checked");
	var length = a.length;
	var keys = "";
	if(length > 0 ){
		for(i=0;i<length;i++){
			if(i== (length-1)){
				keys+= a[i].value;
			}else{
				keys+= a[i].value+",";
			}
		}
	}
	if(length == 0 ){
		alertMsg.info('注意：你并没有选择特定的用户，将不能向特定用户定向发放优惠券。');
	}
	if(length == 1){
		var path = "<%=path%>/favourable/addFavourable.action?totalNum=${paginator.totalCount}&keys="+keys;
		$.pdialog.open(path, 'addFavourableDialog', '发放优惠券',max=true, mask=true);
	}
	else{
		if( (customerId != null && customerId != '') || (customerName != null && customerName != '') ){
			var path = "<%=path%>/favourable/addFavourable.action?totalNum=${paginator.totalCount}";
			path = getSearchParam(path,keys);
			$.pdialog.open(path, 'addFavourableDialog', '发放优惠券',max=true, mask=true);
			return true;
		}else{
			alertMsg.error('请输入商户名称作为查询条件查询。');
			return false;
		}
	}
}

function getSearchParam(path,keys){
	var paramList = jQuery('.param');
	var size = paramList.size();
	for(var i =0;i<size;i++){
		if(paramList[i].value !=null && paramList[i].value != ''){
			var pointIndex = paramList[i].name.indexOf('.');
			var paramName = paramList[i].name.substring(pointIndex+1);
			var value =encodeURI(encodeURI(paramList[i].value));
			path+='&'+paramName+'='+value;
		}
	}
	if(keys != ""){
		path+='&keys='+keys;
	}
	return path;
}
		
function sendMsg(type,user){
	var checkValue = "";
	$('input[type="checkbox"][name="selectids"]:checked').each(
		function(){ 
		     checkValue += $(this).val() + ","; 
		}
	);
	if(user == 'ALL'){
		var op = {okName:$.regional.alertMsg.butMsg.ok, okCall:function(){send("ALL",type);}, cancelName:$.regional.alertMsg.butMsg.cancel, cancelCall:null};
		alertMsg.confirm('确定要给所有会员'+ (type == 'MESSAGE' ?　"推送消息" : "发送短信") +'吗？',op);
		return;
	}else{
		if(checkValue == ""){
			alertMsg.warn("请选择记录");
			return;
		}else{
			send(checkValue, type);
		}
	}
}

function send(checkValue,type){
	var url = "<%=path %>/sendMsg/sendMsg.action?ids="+checkValue;
	if(type == 'MESSAGE'){
		url = "<%=path %>/message/sendMessage.action?ids="+checkValue;
	}
	var options = {mask:true,mixable:true,minable:true,resizable:true}; 
	var s = checkValue == "ALL" ? "所有会员" : "ID【"+checkValue+"】";
	var title = "给"+ s +"发送" + (type == 'MESSAGE' ?　"消息" : "短信");
	var dialogId = type == 'MESSAGE' ? "messageDialog" : "sendMsgDialog"; 
	$.pdialog.open(url, dialogId,title,options);
}

function recharge(){
	var checkValue = "";
	$('input[type="checkbox"][name="selectids"]:checked').each(
		function(){ 
		     checkValue += $(this).val() + ","; 
		}
	);
	if(checkValue == ""){
		alertMsg.error('请选择会员卡');
		return;
	}else{
		var url = "<%=path %>/memberCard/rechargeCard.action?ids="+checkValue+"&type=MEMBERCARD";
		var options = {mask:true,mixable:true,minable:true,resizable:true}; 
		$.pdialog.open(url, "rechargeDialog", "充值",options);
	}
}

//会员卡延期或升级
function deferOrUpgradeMemberCard(type){
	var checkValue = "";
	var checkedObj=$('input[type="checkbox"][name="selectids"]:checked');
	if(checkedObj.length>1){
		alertMsg.error("只能选择一个会员卡");
		return;
	}
	if(checkedObj.length==0){
		alertMsg.error("请选择一个会员卡");
		return;
	}
	var url = "<%=path%>/jsp/membercard/deferMemberCard.jsp?id="+checkedObj.val()+"&expiryDateFrom="+checkedObj.attr("expiryDateFrom")+"&expiryDateTo="+checkedObj.attr("expiryDateTo");
	var tips = "会员卡延期";
	if (type == 1) {
		url = "<%=path%>/jsp/membercard/upgradeMemberCard.jsp?id="+checkedObj.val()+"&customerId="+checkedObj.attr("customerId")+"&cardId="+checkedObj.attr("cardId");
		tips = "会员卡升级";
	}
	var options = {mask:true,mixable:true,minable:true,resizable:true};
	$.pdialog.open(url, "deferOrUpgradeMemberCardDialog", tips, options);
}

function submitSelectForm(form){
	if('${searchType}'=='')
		return navTabSearch(form);
	else
		return dwzSearch(form, 'dialog');
}

function reloadPage() {
	$("#pageSearchForm").submit();
}
</script>
<form id="pagerForm" method="post" action="<%=path%>/memberCard/listMemberCard.action">
	<input type="hidden" name="pageRequest" value="1" />
	<input type="hidden" name="pageNum" value="${paginator.pageIndex}" />
	<input type="hidden" name="numPerPage" value="${paginator.pageSize}" />
</form>

<div class="pageHeader">
	<form id="pageSearchForm" onsubmit="return submitSelectForm(this);" rel="pagerForm" action="<%=path%>/memberCard/listMemberCard.action" method="post">
	<input type="hidden" name="searchType" value="${searchType}"/>
	<div class="searchBar">
		<ul class="searchContent">
			<li>
				<label style="text-align: right;">会员卡号：</label>
				<input name="memberCardVO.schemeCode" value="<s:property value="memberCardVO.schemeCode" />" type="text" class="param"/>
			</li>
			<li>
				<label style="text-align: right;">会员姓名：</label>
				<input name="memberCardVO.memberName"  type="text" class="param" value="<s:property value="memberCardVO.memberName" />"/>
			</li>
			<li>
				<label style="text-align: right;">会员手机：</label>
				<input name="memberCardVO.memberPhone" value="<s:property value="memberCardVO.memberPhone" />" type="text" class="param"/>
			</li>
		</ul>
		<ul class="searchContent">
			<li>
				<label style="text-align: right;">会员身份证号：</label>
				<input name="memberCardVO.membeIdCard" value="<s:property value="memberCardVO.membeIdCard" />" type="text" class="param"/>
			</li>
			<li>
				<label style="text-align: right;">余额：</label>
				从<input name="memberCardVO.gtcardBalance" value="<s:property value="memberCardVO.gtcardBalance" />" type="text" size="5" class="param"/>
				到<input name="memberCardVO.ltcardBalance" value="<s:property value="memberCardVO.ltcardBalance" />" type="text" size="5" class="param"/>
			</li>
			<li>
				<label style="text-align: right;">积分：</label>
				从<input name="memberCardVO.gtscore" value="<s:property value="memberCardVO.gtscore" />" type="text" size="5" class="param"/>
				到<input name="memberCardVO.ltscore" value="<s:property value="memberCardVO.ltscore" />" type="text" size="5" class="param"/>
			</li>
		</ul>
		<ul class="searchContent">
			<li>
				<label style="text-align: right;">会员性别：</label>
				<select name="memberCardVO.memberSex" class="combox param">
					<option value="">全部</option>
					<option value="0"${memberCardVO.memberSex==0?" selected='true'":""}>女</option>
					<option value="1"${memberCardVO.memberSex==1?" selected='true'":""}>男</option>
				</select>
			</li>
			<li>
				<label style="text-align: right;">发卡方式：</label>
				<select name="memberCardVO.sendType" class="combox param">
					<option value="">全部</option>
					<option value="0"${memberCardVO.sendType==0?" selected='true'":""}>商家发放</option>
					<option value="1"${memberCardVO.sendType==1?" selected='true'":""}>自主申请</option>
				</select>
			</li>
			<li>
				<label style="text-align: right;">快捷查询：</label>
				<select name="memberCardVO.yiweiquickSearch" class="combox param">
					<option value="">全部会员</option>
					<option value="0" ${memberCardVO.yiweiquickSearch ==0 ? 'selected="true"' :''}>今日生日会员</option>
					<option value="1" ${memberCardVO.yiweiquickSearch ==1 ? 'selected="true"' :''}>明天生日会员</option>
					<option value="2" ${memberCardVO.yiweiquickSearch ==2 ? 'selected="true"' :''}>本周生日会员</option>
					<option value="3" ${memberCardVO.yiweiquickSearch ==3 ? 'selected="true"' :''}>今日新增会员</option>
					<option value="4" ${memberCardVO.yiweiquickSearch ==4 ? 'selected="true"' :''}>本周新增会员</option>
					<option value="5" ${memberCardVO.yiweiquickSearch ==5 ? 'selected="true"' :''}>超过30天未消费会员</option>
				</select>
			</li>
		</ul>
		<ul class="searchContent">
			<li>
				<label style="text-align: right;">优惠券数量：</label>
				从<input name="memberCardVO.gtcardCouponAmount" value="<s:property value="memberCardVO.gtcardCouponAmount" />" type="text" size="5" class="param"/>
				到<input name="memberCardVO.ltcardCouponAmount" value="<s:property value="memberCardVO.ltcardCouponAmount" />" type="text" size="5" class="param"/>
			</li>
			<li>
				<label style="text-align: right;">计次数量：</label>
				从<input name="memberCardVO.gtcardMeteringAmount" value="<s:property value="memberCardVO.gtcardMeteringAmount" />" type="text" size="5" class="param"/>
				到<input name="memberCardVO.ltcardMeteringAmount" value="<s:property value="memberCardVO.ltcardMeteringAmount" />" type="text" size="5" class="param"/>
			</li>
			<c:choose>
				<c:when test="${SESSION_USER.customer==null}">
					<input name="memberCardVO.customerId" value="${memberCardVO.customerId}" type="hidden">
					<c:if test="${searchType==null||searchType==''}">
						<li>
							<label style="text-align: right;">所属商家：</label>
							<input style="cursor: pointer;float: left;" onclick="$('#memberCardList_a_btnLook').click();" name="memberCardVO.customerName" value="<s:property value="memberCardVO.customerName" />"/>
							<!-- <a class="btnLook" id="memberCardList_a_btnLook" href="<%=path%>/customer/listCustomer.action?searchType=active" lookupGroup="memberCardVO">选择</a> -->
						</li>
					</c:if>
				</c:when>
				<c:otherwise>
					<input name="memberCardVO.customerId" value="${SESSION_USER.customer.id}" type="hidden">
				</c:otherwise>
			</c:choose>
		</ul>
		<ul class="searchContent">
			<li>
				<label style="text-align: right;">有效日期：</label>
				从<input type="text" name="memberCardVO.expiryDateFrom" value="<s:date format="yyyy-MM-dd" name="memberCardVO.expiryDateFrom"/>" class="date param" dateFmt="yyyy-MM-dd" readonly="true" size="10"/>
				到<input type="text" name="memberCardVO.expiryDateTo" value="<s:date format="yyyy-MM-dd" name="memberCardVO.expiryDateTo"/>" class="date param" dateFmt="yyyy-MM-dd" readonly="true" size="10"/>
			</li>
			<li style="width:450px">
				<label style="text-align: right;">发卡时间：</label>
				从<input type="text" name="memberCardVO.gtaddTime" value='<s:date name='memberCardVO.gtaddTime' format="yyyy-MM-dd HH:mm:ss"/>' dateFmt="yyyy-MM-dd HH:mm:ss" class="date param" readonly="false" size="19"/>
				到<input type="text" name="memberCardVO.ltaddTime" value='<s:date name='memberCardVO.ltaddTime' format="yyyy-MM-dd HH:mm:ss"/>' dateFmt="yyyy-MM-dd HH:mm:ss" class="date param" readonly="false" size="19"/>
			</li>
		</ul>
		<div class="subBar">
			<ul>
				<li><div class="buttonActive"><div class="buttonContent"><button type="submit">查询</button></div></div></li>
			</ul>
		</div>
	</div>
	</form>
</div>
<div class="pageContent">
	<c:if test="${searchType==null || searchType==''}">
	<div class="panelBar">
		<ul class="toolBar">
			<shiro:hasPermission name="editMemberCard">
			<li><a class="edit" href="<%=path%>/memberCard/editMemberCard.action?memberCardVO.id={sid_user}" target="navTab" warn="请选择一条记录"><span>修改</span></a></li>
			</shiro:hasPermission> 
			<shiro:hasPermission name="deleteMemberCard">
			<li><a title="确实要删除这些记录吗?" target="selectedTodo" tbName="member_card" rel="selectids" postType="string" href="<%=path%>/memberCard/deleteMemberCard.action" class="delete"><span>删除</span></a></li>
			</shiro:hasPermission>
			<shiro:hasPermission name="addFavourable">
			<li><a class="add"  onclick='return releaseFavourable();'><span>发放优惠券</span></a></li>
			</shiro:hasPermission>
			<shiro:hasPermission name="sendMsg">
			<li><a rel="selectids" href="javascript:sendMsg('SMS','BATCH');" mask=true postType="string" warn="请选择记录" class="edit"><span>发送短信</span></a></li>
			</shiro:hasPermission>
			<shiro:hasPermission name="sendMsg">
			<li><a rel="selectids" href="javascript:sendMsg('SMS','ALL');" mask=true postType="string" warn="请选择记录" class="edit"><span>全部发送短信</span></a></li>
			</shiro:hasPermission>
			<shiro:hasPermission name="sendMessage">
			<li><a rel="selectids" href="javascript:sendMsg('MESSAGE','BATCH');" mask=true postType="string" warn="请选择记录" class="edit"><span>推送消息</span></a></li>
			</shiro:hasPermission>
			<shiro:hasPermission name="sendMessage">
			<li><a rel="selectids" href="javascript:sendMsg('MESSAGE','ALL');" mask=true postType="string" warn="请选择记录" class="edit"><span>全部推送消息</span></a></li>
			</shiro:hasPermission>
			<shiro:hasPermission name="rechargeCard">
			<li><a rel="selectids" href="javascript:recharge();" mask=true postType="string" warn="请选择记录" class="delete"><span>充值</span></a></li>
			</shiro:hasPermission>
			<shiro:hasPermission name="addCost">
			<li><a class="edit" href="<%=path %>/cost/addCost.action?costVO.cardEntity.id={sid_user}" target="navTab" warn="请选择一条记录"><span>消费</span></a></li>
			</shiro:hasPermission>
			<shiro:hasPermission name="addCost">
			<li><a class="edit" href="<%=path %>/membercardcostcount/listMemberCardCostCount.action?costVO.cardEntity.id={sid_user}" target="navTab" warn="请选择一条记录"><span>扣次消费</span></a></li>
			</shiro:hasPermission>
			<shiro:hasPermission name="editMemberService">
				<!-- <li><a class="edit" href="<%=path %>/membercardService/addMembercardService.action?membercardServiceVO.memberCard.id={sid_user}" target="navTab" warn="请选择一条记录"><span>服务计划</span></a></li> -->
				<li><a class="edit" href="<%=path %>/membercardService/listMembercardService.action?membercardServiceVO.memberCard.id={sid_user}" target="navTab" warn="请选择一条记录"><span>服务计划</span></a></li>
			</shiro:hasPermission>
			<shiro:hasPermission name="deferMemberCard">
				<li><a rel="selectids" href="javascript:deferOrUpgradeMemberCard(0);" mask=true postType="string" warn="请选择一条记录" class="edit"><span>会员卡延期</span></a></li>
			</shiro:hasPermission>
			<shiro:hasPermission name="deferMemberCard">
				<li><a rel="selectids" href="javascript:deferOrUpgradeMemberCard(1);" mask=true postType="string" warn="请选择一条记录" class="edit"><span>会员卡升级</span></a></li>
			</shiro:hasPermission>
		</ul>
	</div>
	</c:if>
	<table class="table" width="2000" layoutH="239">
		<thead>
			<tr>
				<th width="30"><input type="checkbox" group="selectids" class="checkboxCtrl"></th>
				<th>会员卡方案编码</th>
				<th>发卡商家</th>
				<th>会员卡编码</th>
				<th>发卡方式</th>
				<th>姓名</th>
				<th>性别</th>
				<th>出生日期</th>
				<th>发卡时间</th>
				<th>手机号码</th>
				<th>会员卡有效期</th>
				<th>Email</th>
				<th>QQ号码</th>
				<th>联系地址</th>
				<th>身份证号</th>
				<th>会员卡余额</th>
				<th>会员卡积分</th>
				<th>会员卡计次数量</th>
				<th>会员卡优惠券数量</th>
				<th>是否手机会员卡</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
			<s:iterator value="memberCardList" id="memberCard" status="st">
			<tr target="sid_user" rel="<s:property value="id" />">
				<td><input name="selectids" value="<s:property value="id" />" type="checkbox" expiryDateFrom="<s:date name="expiryDateFrom" format="yyyy-MM-dd"/>" expiryDateTo="<s:date name="expiryDateTo" format="yyyy-MM-dd"/>" customerId="${customerId}" cardId="${cardId}"></td> 
            	<td><s:property value="cardCode" /></td>
            	<td>${customerVO.name}</td>
            	<td><s:property value="schemeCode" /></td>
            	<td>${sendType==0?"商家自发":"自主申请"}</td>
            	<td><s:property value="memberName" /></td>
            	<td>
            		<s:if test="%{#memberCard.memberSex == 1}">男</s:if>
                	<s:elseif test="%{#memberCard.memberSex == 0}">女</s:elseif>
             	</td>
             	<td><s:date name="memberBirth" format="yyyy-MM-dd"/></td>
             	<td><s:date name="addTime" format="yyyy-MM-dd HH:mm:ss"/></td>
             	<td><s:property value="memberPhone" /></td>
             	<td><s:date name="expiryDateFrom" format="yyyy-MM-dd"/>至<s:date name="expiryDateTo" format="yyyy-MM-dd"/></td>
             	<td><s:property value="memberEmail" /></td>	
             	<td><s:property value="memberQq" /></td>	
             	<td><s:property value="memberAddress" /></td>	
             	<td><s:property value="membeIdCard" /></td>
             	<td>${cardBalance == null ? 0 : cardBalance}</td>
             	<td>${score == null ? 0 : score}</td>
             	<td>
             		<s:if test="%{cardMeteringAmount==null || cardMeteringAmount==0}">0</s:if>
             		<s:else><a href="<%=path%>/membercardcostcount/listMemberCardCostCountView.action?costVO.memberCardId=${id}" target="dialog" rel="activeList" max="true" title="计次详情" width="100%" height="100%">${cardMeteringAmount}</a></s:else>
             	</td>
             	<td>${cardCouponAmount == null ? 0 : cardCouponAmount}</td>
              	<td>
              		<s:if test="%{#memberCard.isMobileUser == 1}">是</s:if>
                	<s:elseif test="%{#memberCard.isMobileUser == 0}">否</s:elseif>
             	</td>
				<td>
					<c:if test="${searchType==null||searchType==''}">
						<shiro:hasPermission name="deleteMemberCard">
							<a title="确实要删除这条记录吗?" target="ajaxTodo" tbName="member_card" ids="${id}" href="<%=path%>/memberCard/deleteMemberCard.action?ids=<s:property value="id" />" class="btnDel">删除</a>
						</shiro:hasPermission>
						<shiro:hasPermission name="editMemberCard">
							<a title="编辑" target="navTab" href="<%=path%>/memberCard/editMemberCard.action?MemberCardVO.id=<s:property value="id" />" class="btnEdit">编辑</a>
						</shiro:hasPermission>
						<shiro:hasPermission name="viewMemberCard">
							<a title="查看" target="navTab" href="<%=path%>/memberCard/viewMemberCard.action?MemberCardVO.id=<s:property value="id" />" class="btnView">查看</a>
						</shiro:hasPermission>
					</c:if>
					<c:if test="${searchType == 'lookup'}">
						<a class="btnSelect" href="javascript:$.bringBack({memberCardId:'${id}', memberName:'${memberName}',memberCode:'${schemeCode}'})" title="查找带回">选择</a>
					</c:if>
					<c:if test="${searchType == 'message'}">
						<a class="btnSelect" href="javascript:$.bringBack({users:'${id}', schemeCode:'${schemeCode}'})" title="查找带回">选择</a>
					</c:if>
				</td>
			</tr>
			</s:iterator>
		</tbody>
	</table>
	<div class="panelBar">
		<div class="pages">
				<span>显示</span>
				<select class="combox" name="numPerPage" onchange='<s:if test="%{searchType==null||searchType==''}">navTabPageBreak({numPerPage:this.value})</s:if><s:else>dialogPageBreak({numPerPage:this.value})</s:else>'>
					<option value="15" <c:if test="${paginator.pageSize==15}">selected='selected'</c:if> >15</option>
					<option value="30" <c:if test="${paginator.pageSize==30}">selected='selected'</c:if> >30</option>
					<option value="50" <c:if test="${paginator.pageSize==50}">selected='selected'</c:if> >50</option>
					<option value="100" <c:if test="${paginator.pageSize==100}">selected='selected'</c:if> >100</option>
					<option value="200" <c:if test="${paginator.pageSize==200}">selected='selected'</c:if> >200</option>
				</select>
				<span>条，共${paginator.totalCount}条</span>
			</div>
			<div class="pagination" targetType="${(searchType==null||searchType=='')?'navTab':'dialog'}" totalCount="${paginator.totalCount}" 
				numPerPage="${paginator.pageSize}" pageNumShown="10" currentPage="${paginator.pageIndex}"></div>
	</div>
</div>